package com.example.demo.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.Dict;
import com.example.demo.base.CommonResult;
import com.example.demo.dto.IdsParam;
import com.example.demo.dto.MenuPageParam;
import com.example.demo.dto.MenuParam;
import com.example.demo.entity.Dept;
import com.example.demo.service.MenuService;
import com.example.demo.validation.Groups;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 系统管理-菜单权限表 前端控制器
 * </p>
 *
 * @author vicente
 * @since 2024-01-24
 */
@Api(tags = "菜单管理模块")
@RestController
@RequestMapping("/demo/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 添加菜单
     * @param param
     * @return
     */
    @PostMapping("/sys/menu/save")
    @ApiOperation(value = "添加菜单")
    @SaCheckPermission("sys:menu:save")
    public CommonResult<?> save(@RequestBody @Validated({Groups.Save.class}) MenuParam param) {
        menuService.save(param);
        return CommonResult.ok();
    }

    /**
     * 删除菜单
     * @param param
     * @return
     */
    @PostMapping("/sys/menu/remove")
    @ApiOperation(value = "删除菜单")
    @SaCheckPermission("sys:menu:remove")
    public CommonResult<?> remove(@RequestBody IdsParam param) {
        menuService.removeBatchByIds(param.getIds());
        return CommonResult.ok();
    }


    @PostMapping("/sys/menu/appList")
    @ApiOperation(value="应用列表")
    public CommonResult<List<Dict>> appList() {
        return CommonResult.data(menuService.appList());
    }
    /**
     *查询菜单树
     * @param param
     * @return
     */
    @PostMapping("/sys/menu/tree")
    @ApiOperation(value = "查询菜单树")
    @SaCheckPermission("sys:menu:tree")
    public CommonResult<List<Dept>> tree(@RequestBody MenuPageParam param) {
        return CommonResult.data(menuService.tree(param));
    }

}
